//
//  ChoiceAndPrintCardViewModel.m
//  carcareIOS
//
//  Created by 王然 on 16/9/20.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MineMoveCardViewModel.h"
#import "MineMoveCardViewController.h"

#import "MoveCarInputViewModel.h"
#import "MoveCarInputViewController.h"
#import "MoveCarSettingViewModel.h"
#import "MoveCarSettingViewController.h"

#import "MoveCarPrintViewController.h"

#import "CarConditionViewController.h"
#import "BaseWebViewController.h"
@interface MineMoveCardViewModel()

@property (nonatomic , strong) MoveCarManager * moveCarMgr;

//我的挪车卡页面 是否返回上一个页面
@property (nonatomic , assign) BOOL isMyCarBackBool;

@end

@implementation MineMoveCardViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        __weak __typeof(self) wself = self;
        self.moveCarMgr = [MoveCarManager sharedInstance];
//        RACSignal * modelValidSignal = RACObserve(self.moveCarMgr , qrs);
//        [modelValidSignal subscribeNext:^(NSArray * models) {
//            if (models) {
//                [((MineMoveCardViewController *)wself.viewController).collectionV reloadData];
//            }else{
////                [self.mgr getNoBotherInfo];
//            }
//        }];
        
        [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"move_car_qrs_update" object:nil]
          takeUntil:[self rac_willDeallocSignal]]
         subscribeNext:^(id x) {
             NSLog(@"Notification received");
             [((MineMoveCardViewController *)wself.viewController).collectionV reloadData];
             ((MineMoveCardViewController *)wself.viewController).pageControl.numberOfPages = [self myCardCollectionCellCount];
             [((MineMoveCardViewController *)wself.viewController).pageControl setHidden:[self myCardCollectionCellCount] < 2];
         }];
        
    }
    return self;
}

//点击 我的挪车卡 右侧 添加按钮；
- (void)clickOnNavAdd
{
    MoveCarInputViewModel * viewModel = [[MoveCarInputViewModel alloc]init];
    
    MoveCarInputViewController *vc = [[[MoveCarInputViewController alloc] initWithViewModel:viewModel] navSetupBackItemWithIdentification:BACK_GRAY_ICON];
    [((MineMoveCardViewController *)self.viewController).navigationController pushViewController:vc animated:YES];
}
//点击 我的挪车卡 右侧 设置按钮；
- (void)clickOnNavSetting
{
    MoveCarSettingViewModel * viewModel = [[MoveCarSettingViewModel alloc]init];
    MoveCarSettingViewController * vc = [[MoveCarSettingViewController alloc] initWithViewModel:viewModel];
    [((MineMoveCardViewController *)self.viewController).navigationController pushViewController:[vc navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
}
//点击 我的挪车卡页面 删除 更换样式 编辑信息 保存 等按钮
- (void)clickOnDeleteOrChangeAndSoOn:(NSInteger)index
{
    MoveCarQRModel * model = [self myCardGetCollectionModelIndex:((MineMoveCardViewController *)self.viewController).selectMyCarIndex];
    switch (index) {
        case 0://删除
        {
            __weak __typeof(self) wself = self;
            TipsView *tips = [TipsView textTipsWithTitle:@"提示" lightTitles:@[[NSString stringWithFormat:@"您确定要删除%@的挪车卡吗？", model.carno]]];
            [tips addLeftButton:@"取消" clickLeft:^{
                [AlertMgr dismiss:TYPE_VIEW_ALERT];
            } rightButton:@"确定" clickRight:^{
                [AlertMgr dismiss:TYPE_VIEW_ALERT];

                [NetMoveCar delQRWithQrID:[model.id integerValue]success:^{
                    [wself.moveCarMgr updateQRListWithSuccess:^{
                        
                    } fail:^{
                        
                    }];
                } fail:^{
                    
                }];
            }];
            [AlertMgr showView:tips];
        }
            break;
        case 1://更换样式
        {
            ChoiceMoveCardViewController *vc = [[[ChoiceMoveCardViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL] navSetupBackItemWithIdentification:BACK_ICON];
            [vc reloadMoveCarQRModel:model upDateStyleId:nil];
            [((MineMoveCardViewController *)self.viewController).navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2://编辑信息
        {
            MoveCarInputViewModel * viewModel = [[MoveCarInputViewModel alloc]init];
            
            MoveCarInputViewController *vc = [[[MoveCarInputViewController alloc] initWithViewModel:viewModel] navSetupBackItemWithIdentification:BACK_GRAY_ICON];
            vc.moveCarQRModel = model;
            [((MineMoveCardViewController *)self.viewController).navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3://保存
        {
            MoveCarPrintViewController * vc = [[[MoveCarPrintViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL] navSetupBackItemWithIdentification:BACK_ICON];
            if(model.styleModel.showQRImage)vc.iconImage = [MoveCarManager createPrintQRWithTypeImage:model.styleModel.showQRImage];
            [((MineMoveCardViewController *)self.viewController).navigationController pushViewController:vc animated:YES];

        }
            break;
        default:
            break;
    }
}
//挪车使用说明
- (void)moveCarTipsClick
{
    BaseWebViewController *webVC = [[BaseWebViewController alloc]initWithURL:@"http://r.ichezheng.com/cz/iomobile/www/app/pages/movecar.instruction.html"];
    [webVC navSetupLeftTitle:@"关闭" action:^{
        [webVC dismissViewControllerAnimated:YES completion:nil];
    }];
    [((MineMoveCardViewController *)self.viewController) presentViewController:[UINavigationController navigationControllerWithRootViewController:webVC] animated:YES completion:nil];
}
#pragma collection delegate  使用
- (MoveCarQRModel *)myCardGetCollectionModelIndex:(NSInteger)index
{
    return self.moveCarMgr.qrs[index];
}
- (NSInteger)myCardCollectionCellCount
{
    return self.moveCarMgr.qrs.count;
}
- (void)myCarGetSelectIndex:(NSInteger)index
{
    if(index >= [self myCardCollectionCellCount])
    {
        index = [self myCardCollectionCellCount] - 1;
    }else if(index < 0) index = 0;
    ((MineMoveCardViewController *)self.viewController).selectMyCarIndex = index;
}

//我的挪车卡页面将要显示
- (void)myCarWillAppear
{
    if (self.moveCarMgr.qrs.count == 0) {
        __weak __typeof(self) wself = self;
        MoveCarInputViewModel * viewModel = [[MoveCarInputViewModel alloc]init];
        MoveCarInputViewController *vc = [[[MoveCarInputViewController alloc] initWithViewModel:viewModel] navSetupBackItemWithIdentification:BACK_GRAY_ICON];
        vc.willBackClick = ^(BOOL isBack)
        {
            if (isBack) {
                wself.isMyCarBackBool = isBack ;
            }
        };
        if (self.isMyCarBackBool && [self myCardCollectionCellCount] == 0) {
            [((MineMoveCardViewController *)self.viewController).navigationController popViewControllerAnimated:YES];
        }else{
            [((MineMoveCardViewController *)self.viewController).navigationController pushViewController:vc animated:YES];
        }
        
    }
}


@end
